<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{
        category.top.name
      }}</XtxBreadItem>
    </Transition>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem v-if="category.sub" :key="category.sub.id">{{
        category.sub.name
      }}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>

<script>
import { computed } from 'vue-demi'
import { useRoute } from 'vue-router'
import store from '@/store'
export default {
  name: 'SubBread',
  setup() {
    const route = useRoute()
    // 3. 通过计算属性得到，二级类目的名称和ID，一级类目的名称和ID
    const category = computed(() => {
      const cate = {}
      store.state.category.list.forEach((top) => {
        if (top.children) {
          top.children.forEach((sub) => {
            if (sub.id === route.params.id) {
              cate.sub = { id: sub.id, name: sub.name }
              // 设置一级类目
              cate.top = { id: top.id, name: top.name }
            }
            // 设置二级类目
          })
        }
      })
      return cate
    })

    return { category }
  },
}
</script>

<style lang="less" scoped>
</style>